package com.example.demo.lambda;


import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author xiao
 * @Date 2025/5/4 上午 9:43
 * @ClassName: CompletableDemo
 * @Desc:
 */
@Slf4j
public class CompletableDemo {

    public static void main(String[] args) throws IOException {
        log.info("开始处理");

       /* //同步处理
        Demo((map) ->{
            map.entrySet().stream()
                    .map(entry -> Map.entry(
                            entry.getKey(),
                            entry.getValue().entrySet().stream().max(Map.Entry.comparingByValue())
                    ))
                    .forEach(System.out::println);
        });
        //有返回值
        CompletableFuture.supplyAsync(() -> {
            log.info("处理消息");
            return "处理完毕111111111111111111111111";
        }).thenAccept(log::info) ;*/
        CompletableFuture.supplyAsync(CompletableDemo::Demo)
                        .thenAccept(r ->r.entrySet().stream().map(entry -> Map.entry(
                                        entry.getKey(),
                                        entry.getValue().entrySet().stream().max(Map.Entry.comparingByValue())))
                                        .forEach(e->log.info(e.toString())));

        log.info("处理完成");
        System.in.read();

    }

    public static Map<String, Map<String, Long>> Demo(/*Consumer<Map<String, Map<String, Long>>> consumer*/) {
        try (Stream<String> stream = Files.lines(Paths.get("order.txt"))) {
          // 获取每个地区销量第一
          //  原始数据
            Map<String, Map<String, Long>> map = stream.map(s -> s.split(","))
                    .collect(Collectors.groupingBy(array -> array[OrderFieldEnum.addr.getField()],
                            Collectors.groupingBy(array -> array[OrderFieldEnum.goodsId.getField()], Collectors.counting())));
            return map;
        /*// 处理逻辑
            consumer.accept(map);*/
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
